#*****************************************************************************************
#* 2019-10-05
#* Ingrid Mauerer
#* The Neglected Role and Variability of Party Intercepts in the Spatial Valence Approach 
#* Political Analysis
#******************************************************************************************

#******************************************************************************************
# This file contains the commands to run the models and create Figure 1 in R 
# R version 3.5.1 
# Platform: x86_64-w64-mingw32/x64 (64-bit)
#******************************************************************************************


#*************
## Packages
#*************
library(mlogit)
library(ggplot2)

#*************************
## Set working directory
#*************************
setwd("...")

#*************
# Load Data
#************
load("Election_Data.RData")

#*****************************************
#*** I.   Intercept-only models (Table 1)
#*****************************************
  
#*-------------------------------
#* CDU reference party (C)
#*-------------------------------
InterceptC<-mlogit(chosen ~ 0 | 1| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(InterceptC)

# Calculation of vote shares (last column in Table 1): 

# CDU:
predictC<- 1/(1+sum(exp(coef(InterceptC))))
predictC

# SPD:
predictS<-exp(coef(InterceptC)["SPD:(intercept)"])/(1+sum(exp(coef(InterceptC))))
predictS

# FDP:
predictF<-exp(coef(InterceptC)["FDP:(intercept)"])/(1+sum(exp(coef(InterceptC))))
predictF

# Greens:
predictG<-exp(coef(InterceptC)["Greens:(intercept)"])/(1+sum(exp(coef(InterceptC))))
predictG

# Left:
predictL<-exp(coef(InterceptC)["Left:(intercept)"])/(1+sum(exp(coef(InterceptC))))
predictL

#*-------------------------------
#* SPD reference party (S)
#*-------------------------------
InterceptS<-mlogit(chosen ~ 0 | 1| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "SPD")
summary(InterceptS)

#*-------------------------------
#* FDP reference party (F)
#*------------------------------- 
InterceptF<-mlogit(chosen ~ 0 | 1| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "FDP")
summary(InterceptF)

#*-------------------------------
#* Greens reference party (G)
#*------------------------------- 
InterceptG<-mlogit(chosen ~ 0 | 1| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "Greens")
summary(InterceptG) 

#*-------------------------------
#* Left reference party (L)
#*-------------------------------
InterceptL<-mlogit(chosen ~ 0 | 1| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "Left")
summary(InterceptL) 


#*************************
#* Table 1
#*************************

InterceptCSE<-sqrt(diag(solve(-InterceptC$hessian)))
InterceptSSE<-sqrt(diag(solve(-InterceptS$hessian)))
InterceptFSE<-sqrt(diag(solve(-InterceptF$hessian)))
InterceptGSE<-sqrt(diag(solve(-InterceptG$hessian)))
InterceptLSE<-sqrt(diag(solve(-InterceptL$hessian)))

Table1<-matrix(NA,5,12)
colnames(Table1)<-c("Sample Vote Share", "CDU:(intercept)", "CDU:SE", "SPD:(intercept)", "SPD:SE", "FDP:(intercept)", "FDP:SE",
                    "Greens:(intercept)", "Greens:SE", "Left:(intercept)", "Left:SE", "Estimated Vote Share") 
rownames(Table1)<-c("CDU:(intercept)", "SPD:(intercept)", "FDP:(intercept)", "Greens:(intercept)", "Left:(intercept)") 



Table1["CDU:(intercept)","Sample Vote Share"]<-table(Election_Data$vote)["CDU"]/nrow(Election_Data)
Table1["SPD:(intercept)","Sample Vote Share"]<-table(Election_Data$vote)["SPD"]/nrow(Election_Data)
Table1["FDP:(intercept)","Sample Vote Share"]<-table(Election_Data$vote)["FDP"]/nrow(Election_Data)
Table1["Greens:(intercept)","Sample Vote Share"]<-table(Election_Data$vote)["Greens"]/nrow(Election_Data)
Table1["Left:(intercept)","Sample Vote Share"]<-table(Election_Data$vote)["Left"]/nrow(Election_Data)

Table1["SPD:(intercept)","CDU:(intercept)" ]<-coef(InterceptC)["SPD:(intercept)"]
Table1["FDP:(intercept)","CDU:(intercept)" ]<-coef(InterceptC)["FDP:(intercept)"]
Table1["Greens:(intercept)","CDU:(intercept)" ]<-coef(InterceptC)["Greens:(intercept)"]
Table1["Left:(intercept)","CDU:(intercept)" ]<-coef(InterceptC)["Left:(intercept)"]

Table1["SPD:(intercept)","CDU:SE" ]<-InterceptCSE["SPD:(intercept)"]
Table1["FDP:(intercept)","CDU:SE" ]<-InterceptCSE["FDP:(intercept)"]
Table1["Greens:(intercept)","CDU:SE" ]<-InterceptCSE["Greens:(intercept)"]
Table1["Left:(intercept)","CDU:SE" ]<-InterceptCSE["Left:(intercept)"]

Table1["CDU:(intercept)","SPD:(intercept)" ]<-coef(InterceptS)["CDU:(intercept)"]
Table1["FDP:(intercept)","SPD:(intercept)" ]<-coef(InterceptS)["FDP:(intercept)"]
Table1["Greens:(intercept)","SPD:(intercept)" ]<-coef(InterceptS)["Greens:(intercept)"]
Table1["Left:(intercept)","SPD:(intercept)" ]<-coef(InterceptS)["Left:(intercept)"]

Table1["CDU:(intercept)","SPD:SE" ]<-InterceptSSE["CDU:(intercept)"]
Table1["FDP:(intercept)","SPD:SE" ]<-InterceptSSE["FDP:(intercept)"]
Table1["Greens:(intercept)","SPD:SE" ]<-InterceptSSE["Greens:(intercept)"]
Table1["Left:(intercept)","SPD:SE" ]<-InterceptSSE["Left:(intercept)"]

Table1["CDU:(intercept)","FDP:(intercept)" ]<-coef(InterceptF)["CDU:(intercept)"]
Table1["SPD:(intercept)","FDP:(intercept)" ]<-coef(InterceptF)["SPD:(intercept)"]
Table1["Greens:(intercept)","FDP:(intercept)" ]<-coef(InterceptF)["Greens:(intercept)"]
Table1["Left:(intercept)","FDP:(intercept)" ]<-coef(InterceptF)["Left:(intercept)"]

Table1["CDU:(intercept)","FDP:SE" ]<-InterceptFSE["CDU:(intercept)"]
Table1["SPD:(intercept)","FDP:SE" ]<-InterceptFSE["SPD:(intercept)"]
Table1["Greens:(intercept)","FDP:SE" ]<-InterceptFSE["Greens:(intercept)"]
Table1["Left:(intercept)","FDP:SE" ]<-InterceptFSE["Left:(intercept)"]

Table1["CDU:(intercept)","Greens:(intercept)" ]<-coef(InterceptG)["CDU:(intercept)"]
Table1["SPD:(intercept)","Greens:(intercept)" ]<-coef(InterceptG)["SPD:(intercept)"]
Table1["FDP:(intercept)","Greens:(intercept)" ]<-coef(InterceptG)["FDP:(intercept)"]
Table1["Left:(intercept)","Greens:(intercept)" ]<-coef(InterceptG)["Left:(intercept)"]

Table1["CDU:(intercept)","Greens:SE" ]<-InterceptGSE["CDU:(intercept)"]
Table1["SPD:(intercept)","Greens:SE" ]<-InterceptGSE["SPD:(intercept)"]
Table1["FDP:(intercept)","Greens:SE" ]<-InterceptGSE["FDP:(intercept)"]
Table1["Left:(intercept)","Greens:SE" ]<-InterceptGSE["Left:(intercept)"]

Table1["CDU:(intercept)","Left:(intercept)" ]<-coef(InterceptL)["CDU:(intercept)"]
Table1["SPD:(intercept)","Left:(intercept)" ]<-coef(InterceptL)["SPD:(intercept)"]
Table1["FDP:(intercept)","Left:(intercept)" ]<-coef(InterceptL)["FDP:(intercept)"]
Table1["Greens:(intercept)","Left:(intercept)" ]<-coef(InterceptL)["Greens:(intercept)"]

Table1["CDU:(intercept)","Left:SE" ]<-InterceptLSE["CDU:(intercept)"]
Table1["SPD:(intercept)","Left:SE" ]<-InterceptLSE["SPD:(intercept)"]
Table1["FDP:(intercept)","Left:SE" ]<-InterceptLSE["FDP:(intercept)"]
Table1["Greens:(intercept)","Left:SE" ]<-InterceptLSE["Greens:(intercept)"]

Table1["CDU:(intercept)","Estimated Vote Share"]<-predictC
Table1["SPD:(intercept)","Estimated Vote Share" ]<-predictS
Table1["FDP:(intercept)","Estimated Vote Share" ]<-predictF
Table1["Greens:(intercept)","Estimated Vote Share" ]<-predictG
Table1["Left:(intercept)","Estimated Vote Share" ]<-predictL



# Calculation of BIC: 
-2*(-928.4286) + log(715)*4   # = 1883.146
AIC(InterceptC, k = log(715)) # = 1883.146

con <- file("Table1.txt", open="wt")
write.table(Table1, con)
writeLines(paste("N:", nrow(Election_Data)/5,InterceptC$df,  "logLik (df=4):", InterceptC$'logLik', "AIC:", AIC(InterceptC), "BIC:",AIC(InterceptC, k = log(715))), con)
close(con)

Table1


#****************************************
#*** II.   Policy-only model (Table A1)
#****************************************
Policy<-mlogit(chosen ~   imm_ + eu_ + ne_ + lr_ | 1| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(Policy)


#*****************************************************
#*** III. Fully specified models (Tables A3, A4)
#*****************************************************
  
#*-------------------------------
#* CDU reference party (C)
#*-------------------------------
FullC<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(FullC)


#*-------------------------------
#* SPD reference party (S)
#*-------------------------------
FullS<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "SPD")
summary(FullS)

#*-------------------------------
#* FDP reference party (F)
#*------------------------------- 
FullF<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "FDP")
summary(FullF)

#*-------------------------------
#* Greens reference party (G)
#*------------------------------- 
FullG<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "Greens")
summary(FullG)

#*-------------------------------
#* Left reference party (L)
#*-------------------------------
FullL<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "Left")
summary(FullL)


#**********************************************************************
#*** IV. Reversed coding of voter attributes (Figure 1, Tables A5-A8)
#**********************************************************************
  
#*----------------
#* gender (sex2)
#*----------------
Gender<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(Gender)


GenderR<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex2 + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(GenderR)

#*-----------------
#* worker (work2)
#*-----------------
Worker<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(Worker)

WorkerR<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work2 + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(WorkerR)

#*----------------------
#* West Germany (west2)
#*----------------------
West<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(West)

WestR<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west2| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(WestR)

#*----------------------
#* trade union (union2)
#*----------------------
Trade<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(Trade)

TradeR<-mlogit(chosen ~ imm_ + eu_ + ne_ + lr_ | union2 + work + relig + age + sex + west| 0, data=Election_Data, shape="long", alt.var="party", reflevel = "CDU")
summary(TradeR)

#*************************
#* Figure 1
#*************************

# 95% confidence intervals
interval <- -qnorm((1-0.95)/2)  

#-----------------------
# Gender:
#-----------------------
coef.namesG <- c("Gender Left",
                 "Gender Gre",
                 "Gender FDP",
                 "Gender SPD",
                 "Gender CDU",
                 "Intercept Left",
                 "Intercept Gre",
                 "Intercept FDP",
                 "Intercept SPD",
                 "Intercept CDU")

GenderSE<-sqrt(diag(solve(-Gender$hessian)))

# Coding: "female (1), male (0) 
gender.PlotA<-data.frame(Variable=coef.namesG, Coefficient=0, SE=0)

gender.PlotA[gender.PlotA$Variable=="Intercept SPD", "Coefficient"]<-coef(Gender)[c("SPD:(intercept)")]
gender.PlotA[gender.PlotA$Variable=="Intercept FDP", "Coefficient"]<-coef(Gender)[c("FDP:(intercept)")]
gender.PlotA[gender.PlotA$Variable=="Intercept Gre", "Coefficient"]<-coef(Gender)[c("Greens:(intercept)")]
gender.PlotA[gender.PlotA$Variable=="Intercept Left", "Coefficient"]<-coef(Gender)[c("Left:(intercept)")]
gender.PlotA[gender.PlotA$Variable=="Gender SPD", "Coefficient"]<-coef(Gender)[c("SPD:sex")]
gender.PlotA[gender.PlotA$Variable=="Gender FDP", "Coefficient"]<-coef(Gender)[c("FDP:sex")]
gender.PlotA[gender.PlotA$Variable=="Gender Gre", "Coefficient"]<-coef(Gender)[c("Greens:sex")]
gender.PlotA[gender.PlotA$Variable=="Gender Left", "Coefficient"]<-coef(Gender)[c("Left:sex")]

gender.PlotA[gender.PlotA$Variable=="Intercept SPD", "SE"]<-GenderSE[c("SPD:(intercept)")]
gender.PlotA[gender.PlotA$Variable=="Intercept FDP", "SE"]<-GenderSE[c("FDP:(intercept)")]
gender.PlotA[gender.PlotA$Variable=="Intercept Gre", "SE"]<-GenderSE[c("Greens:(intercept)")]
gender.PlotA[gender.PlotA$Variable=="Intercept Left", "SE"]<-GenderSE[c("Left:(intercept)")]
gender.PlotA[gender.PlotA$Variable=="Gender SPD", "SE"]<-GenderSE[c("SPD:sex")]
gender.PlotA[gender.PlotA$Variable=="Gender FDP", "SE"]<-GenderSE[c("FDP:sex")]
gender.PlotA[gender.PlotA$Variable=="Gender Gre", "SE"]<-GenderSE[c("Greens:sex")]
gender.PlotA[gender.PlotA$Variable=="Gender Left", "SE"]<-GenderSE[c("Left:sex")]

gender.PlotA$Coding<-"female (1), male (0)"

gender.PlotA


# Coding: "male (1), female (0)" 
GenderRSE<-sqrt(diag(solve(-GenderR$hessian)))

gender.PlotB<-data.frame(Variable=coef.namesG, Coefficient=0, SE=0)

gender.PlotB[gender.PlotB$Variable=="Intercept SPD", "Coefficient"]<-coef(GenderR)[c("SPD:(intercept)")]
gender.PlotB[gender.PlotB$Variable=="Intercept FDP", "Coefficient"]<-coef(GenderR)[c("FDP:(intercept)")]
gender.PlotB[gender.PlotB$Variable=="Intercept Gre", "Coefficient"]<-coef(GenderR)[c("Greens:(intercept)")]
gender.PlotB[gender.PlotB$Variable=="Intercept Left", "Coefficient"]<-coef(GenderR)[c("Left:(intercept)")]
gender.PlotB[gender.PlotB$Variable=="Gender SPD", "Coefficient"]<-coef(GenderR)[c("SPD:sex2")]
gender.PlotB[gender.PlotB$Variable=="Gender FDP", "Coefficient"]<-coef(GenderR)[c("FDP:sex2")]
gender.PlotB[gender.PlotB$Variable=="Gender Gre", "Coefficient"]<-coef(GenderR)[c("Greens:sex2")]
gender.PlotB[gender.PlotB$Variable=="Gender Left", "Coefficient"]<-coef(GenderR)[c("Left:sex2")]

gender.PlotB[gender.PlotB$Variable=="Intercept SPD", "SE"]<-GenderRSE[c("SPD:(intercept)")]
gender.PlotB[gender.PlotB$Variable=="Intercept FDP", "SE"]<-GenderRSE[c("FDP:(intercept)")]
gender.PlotB[gender.PlotB$Variable=="Intercept Gre", "SE"]<-GenderRSE[c("Greens:(intercept)")]
gender.PlotB[gender.PlotB$Variable=="Intercept Left", "SE"]<-GenderRSE[c("Left:(intercept)")]
gender.PlotB[gender.PlotB$Variable=="Gender SPD", "SE"]<-GenderRSE[c("SPD:sex2")]
gender.PlotB[gender.PlotB$Variable=="Gender FDP", "SE"]<-GenderRSE[c("FDP:sex2")]
gender.PlotB[gender.PlotB$Variable=="Gender Gre", "SE"]<-GenderRSE[c("Greens:sex2")]
gender.PlotB[gender.PlotB$Variable=="Gender Left", "SE"]<-GenderRSE[c("Left:sex2")]

gender.PlotB$Coding<-"male (1), female (0)"
gender.PlotB

gender.Plot<-rbind(gender.PlotA,gender.PlotB)
gender.Plot

gender.Plot$Variable <- factor(gender.Plot$Variable, levels=coef.namesG)

# Plot
pdf(file="Figure1_Gender.pdf",width=6, height=3.7)
par(mar=c(0,0,2.5,0))

GenderP <- ggplot(gender.Plot, aes(color =rev(Coding), group=rev(Coding))) +
  geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_pointrange(aes(x = Variable, y = Coefficient,  ymin = Coefficient - SE*interval, ymax = Coefficient + SE*interval),
                  lwd = 1/2, position = position_dodge(width = 1/2),
                  shape = rep(c(21,22),10), fill = "WHITE") + 
                  scale_y_continuous(name="", limits=c(-5,3), breaks=c(-5,-4,-3,-2,-1,0,1,2,3)) + coord_flip() + theme_bw(base_size = 12) + 
                  theme(legend.position="none", legend.margin = margin(-1,-1,-1,-1), legend.spacing = unit(-1, "cm"), legend.title = element_blank(),) + 
                  labs(x="", y="") + scale_fill_grey(start = 0.6, end = 0.9) + scale_color_grey(start = 0.1, end = 0.3) +
                  guides(fill = guide_legend(override.aes = list(shape = c(21,22))),
                  colour = guide_legend(override.aes = list(size=1,linetype=0, shape = c(21,22)  ))) 
print(GenderP)

dev.off()

#-----------------------
# Worker
#-----------------------
coef.namesW <- c("Worker Left",
                 "Worker Gre",
                 "Worker FDP",
                 "Worker SPD",
                 "Worker CDU",
                 "Intercept Left",
                 "Intercept Gre",
                 "Intercept FDP",
                 "Intercept SPD",
                 "Intercept CDU")

WorkerSE<-sqrt(diag(solve(-Worker$hessian)))

# Coding: worker (1), otherwise (0) 
worker.PlotA<-data.frame(Variable=coef.namesW, Coefficient=0, SE=0)

worker.PlotA[worker.PlotA$Variable=="Intercept SPD", "Coefficient"]<-coef(Worker)[c("SPD:(intercept)")]
worker.PlotA[worker.PlotA$Variable=="Intercept FDP", "Coefficient"]<-coef(Worker)[c("FDP:(intercept)")]
worker.PlotA[worker.PlotA$Variable=="Intercept Gre", "Coefficient"]<-coef(Worker)[c("Greens:(intercept)")]
worker.PlotA[worker.PlotA$Variable=="Intercept Left", "Coefficient"]<-coef(Worker)[c("Left:(intercept)")]
worker.PlotA[worker.PlotA$Variable=="Worker SPD", "Coefficient"]<-coef(Worker)[c("SPD:work")]
worker.PlotA[worker.PlotA$Variable=="Worker FDP", "Coefficient"]<-coef(Worker)[c("FDP:work")]
worker.PlotA[worker.PlotA$Variable=="Worker Gre", "Coefficient"]<-coef(Worker)[c("Greens:work")]
worker.PlotA[worker.PlotA$Variable=="Worker Left", "Coefficient"]<-coef(Worker)[c("Left:work")]

worker.PlotA[worker.PlotA$Variable=="Intercept SPD", "SE"]<-WorkerSE[c("SPD:(intercept)")]
worker.PlotA[worker.PlotA$Variable=="Intercept FDP", "SE"]<-WorkerSE[c("FDP:(intercept)")]
worker.PlotA[worker.PlotA$Variable=="Intercept Gre", "SE"]<-WorkerSE[c("Greens:(intercept)")]
worker.PlotA[worker.PlotA$Variable=="Intercept Left", "SE"]<-WorkerSE[c("Left:(intercept)")]
worker.PlotA[worker.PlotA$Variable=="Worker SPD", "SE"]<-WorkerSE[c("SPD:work")]
worker.PlotA[worker.PlotA$Variable=="Worker FDP", "SE"]<-WorkerSE[c("FDP:work")]
worker.PlotA[worker.PlotA$Variable=="Worker Gre", "SE"]<-WorkerSE[c("Greens:work")]
worker.PlotA[worker.PlotA$Variable=="Worker Left", "SE"]<-WorkerSE[c("Left:work")]

worker.PlotA$Coding<-"worker (1), otherwise (0)"
worker.PlotA

# Coding: "otherwise (1), worker (0)" 
WorkerRSE<-sqrt(diag(solve(-WorkerR$hessian)))

worker.PlotB<-data.frame(Variable=coef.namesW, Coefficient=0, SE=0)

worker.PlotB[worker.PlotB$Variable=="Intercept SPD", "Coefficient"]<-coef(WorkerR)[c("SPD:(intercept)")]
worker.PlotB[worker.PlotB$Variable=="Intercept FDP", "Coefficient"]<-coef(WorkerR)[c("FDP:(intercept)")]
worker.PlotB[worker.PlotB$Variable=="Intercept Gre", "Coefficient"]<-coef(WorkerR)[c("Greens:(intercept)")]
worker.PlotB[worker.PlotB$Variable=="Intercept Left", "Coefficient"]<-coef(WorkerR)[c("Left:(intercept)")]
worker.PlotB[worker.PlotB$Variable=="Worker SPD", "Coefficient"]<-coef(WorkerR)[c("SPD:work2")]
worker.PlotB[worker.PlotB$Variable=="Worker FDP", "Coefficient"]<-coef(WorkerR)[c("FDP:work2")]
worker.PlotB[worker.PlotB$Variable=="Worker Gre", "Coefficient"]<-coef(WorkerR)[c("Greens:work2")]
worker.PlotB[worker.PlotB$Variable=="Worker Left", "Coefficient"]<-coef(WorkerR)[c("Left:work2")]

worker.PlotB[worker.PlotB$Variable=="Intercept SPD", "SE"]<-WorkerRSE[c("SPD:(intercept)")]
worker.PlotB[worker.PlotB$Variable=="Intercept FDP", "SE"]<-WorkerRSE[c("FDP:(intercept)")]
worker.PlotB[worker.PlotB$Variable=="Intercept Gre", "SE"]<-WorkerRSE[c("Greens:(intercept)")]
worker.PlotB[worker.PlotB$Variable=="Intercept Left", "SE"]<-WorkerRSE[c("Left:(intercept)")]
worker.PlotB[worker.PlotB$Variable=="Worker SPD", "SE"]<-WorkerRSE[c("SPD:work2")]
worker.PlotB[worker.PlotB$Variable=="Worker FDP", "SE"]<-WorkerRSE[c("FDP:work2")]
worker.PlotB[worker.PlotB$Variable=="Worker Gre", "SE"]<-WorkerRSE[c("Greens:work2")]
worker.PlotB[worker.PlotB$Variable=="Worker Left", "SE"]<-WorkerRSE[c("Left:work2")]

worker.PlotB$Coding<-"otherwise (1), worker (0)"
worker.PlotB


worker.Plot<-rbind(worker.PlotA,worker.PlotB)
worker.Plot

worker.Plot$Variable <- factor(worker.Plot$Variable, levels=coef.namesW)

# Plot
pdf(file="Figure1_Worker.pdf",width=6, height=3.7)
par(mar=c(0,0,2.5,0)) 

WorkerP <- ggplot(worker.Plot, aes(color =Coding, group=Coding)) + 
  geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_pointrange(aes(x = Variable, y = Coefficient, ymin = Coefficient - SE*interval, ymax = Coefficient + SE*interval),
                  lwd = 1/2, position = position_dodge(width = 1/2),
                  shape = rep(c(21,22),10), fill = "WHITE") + 
                  scale_y_continuous(name="", limits=c(-5,3), breaks=c(-5,-4,-3,-2,-1,0,1,2,3)) + coord_flip() +  theme_bw(base_size = 12) + 
                  theme(legend.position="none", legend.margin = margin(-1,-1,-1,-1), legend.spacing = unit(-1, "cm"), legend.title = element_blank(),) + 
                  labs(x="", y="") + scale_fill_grey(start = 0.6, end = 0.9) + scale_color_grey(start = 0.1, end = 0.3)+
                  guides(fill = guide_legend(override.aes = list(shape = c(21,22))),
                  colour = guide_legend(override.aes = list(size=0.9,linetype=0, shape = c(21,22)  ))) 
print(WorkerP)

dev.off()

#-----------------------
# West Germany
#-----------------------
coef.namesR <- c("West Ger. Left",
                 "West Ger. Gre",
                 "West Ger. FDP",
                 "West Ger. SPD",
                 "West Ger. CDU",
                 "Intercept Left",
                 "Intercept Gre",
                 "Intercept FDP",
                 "Intercept SPD",
                 "Intercept CDU")

WestSE<-sqrt(diag(solve(-West$hessian)))

# Coding: "West Ger. (1), East Ger. (0) 
west.PlotA<-data.frame(Variable=coef.namesR, Coefficient=0, SE=0)

west.PlotA[west.PlotA$Variable=="Intercept SPD", "Coefficient"]<-coef(West)[c("SPD:(intercept)")]
west.PlotA[west.PlotA$Variable=="Intercept FDP", "Coefficient"]<-coef(West)[c("FDP:(intercept)")]
west.PlotA[west.PlotA$Variable=="Intercept Gre", "Coefficient"]<-coef(West)[c("Greens:(intercept)")]
west.PlotA[west.PlotA$Variable=="Intercept Left", "Coefficient"]<-coef(West)[c("Left:(intercept)")]
west.PlotA[west.PlotA$Variable=="West Ger. SPD", "Coefficient"]<-coef(West)[c("SPD:west")]
west.PlotA[west.PlotA$Variable=="West Ger. FDP", "Coefficient"]<-coef(West)[c("FDP:west")]
west.PlotA[west.PlotA$Variable=="West Ger. Gre", "Coefficient"]<-coef(West)[c("Greens:west")]
west.PlotA[west.PlotA$Variable=="West Ger. Left", "Coefficient"]<-coef(West)[c("Left:west")]

west.PlotA[west.PlotA$Variable=="Intercept SPD", "SE"]<-WestSE[c("SPD:(intercept)")]
west.PlotA[west.PlotA$Variable=="Intercept FDP", "SE"]<-WestSE[c("FDP:(intercept)")]
west.PlotA[west.PlotA$Variable=="Intercept Gre", "SE"]<-WestSE[c("Greens:(intercept)")]
west.PlotA[west.PlotA$Variable=="Intercept Left", "SE"]<-WestSE[c("Left:(intercept)")]
west.PlotA[west.PlotA$Variable=="West Ger. SPD", "SE"]<-WestSE[c("SPD:west")]
west.PlotA[west.PlotA$Variable=="West Ger. FDP", "SE"]<-WestSE[c("FDP:west")]
west.PlotA[west.PlotA$Variable=="West Ger. Gre", "SE"]<-WestSE[c("Greens:west")]
west.PlotA[west.PlotA$Variable=="West Ger. Left", "SE"]<-WestSE[c("Left:west")]

west.PlotA$Coding<-"West Ger. (1), East Ger. (0)"
west.PlotA


# Coding: "East Ger. (1), West Ger. (0)" 
WestRSE<-sqrt(diag(solve(-WestR$hessian)))

west.PlotB<-data.frame(Variable=coef.namesR, Coefficient=0, SE=0)

west.PlotB[west.PlotB$Variable=="Intercept SPD", "Coefficient"]<-coef(WestR)[c("SPD:(intercept)")]
west.PlotB[west.PlotB$Variable=="Intercept FDP", "Coefficient"]<-coef(WestR)[c("FDP:(intercept)")]
west.PlotB[west.PlotB$Variable=="Intercept Gre", "Coefficient"]<-coef(WestR)[c("Greens:(intercept)")]
west.PlotB[west.PlotB$Variable=="Intercept Left", "Coefficient"]<-coef(WestR)[c("Left:(intercept)")]
west.PlotB[west.PlotB$Variable=="West Ger. SPD", "Coefficient"]<-coef(WestR)[c("SPD:west2")]
west.PlotB[west.PlotB$Variable=="West Ger. FDP", "Coefficient"]<-coef(WestR)[c("FDP:west2")]
west.PlotB[west.PlotB$Variable=="West Ger. Gre", "Coefficient"]<-coef(WestR)[c("Greens:west2")]
west.PlotB[west.PlotB$Variable=="West Ger. Left", "Coefficient"]<-coef(WestR)[c("Left:west2")]

west.PlotB[west.PlotB$Variable=="Intercept SPD", "SE"]<-WestRSE[c("SPD:(intercept)")]
west.PlotB[west.PlotB$Variable=="Intercept FDP", "SE"]<-WestRSE[c("FDP:(intercept)")]
west.PlotB[west.PlotB$Variable=="Intercept Gre", "SE"]<-WestRSE[c("Greens:(intercept)")]
west.PlotB[west.PlotB$Variable=="Intercept Left", "SE"]<-WestRSE[c("Left:(intercept)")]
west.PlotB[west.PlotB$Variable=="West Ger. SPD", "SE"]<-WestRSE[c("SPD:west2")]
west.PlotB[west.PlotB$Variable=="West Ger. FDP", "SE"]<-WestRSE[c("FDP:west2")]
west.PlotB[west.PlotB$Variable=="West Ger. Gre", "SE"]<-WestRSE[c("Greens:west2")]
west.PlotB[west.PlotB$Variable=="West Ger. Left", "SE"]<-WestRSE[c("Left:west2")]

west.PlotB$Coding<-"East Ger. (1), West Ger. (0)"
west.PlotB


west.Plot<-rbind(west.PlotA,west.PlotB)
west.Plot

west.Plot$Variable <- factor(west.Plot$Variable, levels=coef.namesR)

# Plot
pdf(file="Figure1_West.pdf",width=6, height=3.7)
par(mar=c(0,0,2.5,0)) 

WestP <- ggplot(west.Plot, aes(color =Coding, group=Coding)) + 
  geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_pointrange(aes(x = Variable, y = Coefficient, ymin = Coefficient - SE*interval, ymax = Coefficient + SE*interval),
                  lwd = 1/2, position = position_dodge(width = 1/2),
                  shape = rep(c(21,22),10), fill = "WHITE") + 
                  scale_y_continuous(name="", limits=c(-5,3.2), breaks=c(-5,-4,-3,-2,-1,0,1,2,3)) + coord_flip() + theme_bw(base_size = 12) + 
                  theme(legend.position="none", legend.margin = margin(-1,-1,-1,-1), legend.spacing = unit(-1, "cm"), legend.title = element_blank(),) + 
                  labs(x="", y="") + scale_fill_grey(start = 0.6, end = 0.9) + scale_color_grey(start = 0.1, end = 0.3) +
                  guides(fill = guide_legend(override.aes = list(shape = c(21,22))),
                  colour = guide_legend(override.aes = list(size=0.9,linetype=0, shape = c(21,22)  ))) 
print(WestP)

dev.off()

#-----------------------
# Trade Union 
#-----------------------
coef.namesT <- c("Trade Union Left",
                 "Trade Union Gre",
                 "Trade Union FDP",
                 "Trade Union SPD",
                 "Trade Union CDU",
                 "Intercept Left",
                 "Intercept Gre",
                 "Intercept FDP",
                 "Intercept SPD",
                 "Intercept CDU")

TradeSE<-sqrt(diag(solve(-Trade$hessian)))

# Coding: "member (1), otherwise (0) 
trade.PlotA<-data.frame(Variable=coef.namesT, Coefficient=0, SE=0)

trade.PlotA[trade.PlotA$Variable=="Intercept SPD", "Coefficient"]<-coef(Trade)[c("SPD:(intercept)")]
trade.PlotA[trade.PlotA$Variable=="Intercept FDP", "Coefficient"]<-coef(Trade)[c("FDP:(intercept)")]
trade.PlotA[trade.PlotA$Variable=="Intercept Gre", "Coefficient"]<-coef(Trade)[c("Greens:(intercept)")]
trade.PlotA[trade.PlotA$Variable=="Intercept Left", "Coefficient"]<-coef(Trade)[c("Left:(intercept)")]
trade.PlotA[trade.PlotA$Variable=="Trade Union SPD", "Coefficient"]<-coef(Trade)[c("SPD:union")]
trade.PlotA[trade.PlotA$Variable=="Trade Union FDP", "Coefficient"]<-coef(Trade)[c("FDP:union")]
trade.PlotA[trade.PlotA$Variable=="Trade Union Gre", "Coefficient"]<-coef(Trade)[c("Greens:union")]
trade.PlotA[trade.PlotA$Variable=="Trade Union Left", "Coefficient"]<-coef(Trade)[c("Left:union")]

trade.PlotA[trade.PlotA$Variable=="Intercept SPD", "SE"]<-TradeSE[c("SPD:(intercept)")]
trade.PlotA[trade.PlotA$Variable=="Intercept FDP", "SE"]<-TradeSE[c("FDP:(intercept)")]
trade.PlotA[trade.PlotA$Variable=="Intercept Gre", "SE"]<-TradeSE[c("Greens:(intercept)")]
trade.PlotA[trade.PlotA$Variable=="Intercept Left", "SE"]<-TradeSE[c("Left:(intercept)")]
trade.PlotA[trade.PlotA$Variable=="Trade Union SPD", "SE"]<-TradeSE[c("SPD:union")]
trade.PlotA[trade.PlotA$Variable=="Trade Union FDP", "SE"]<-TradeSE[c("FDP:union")]
trade.PlotA[trade.PlotA$Variable=="Trade Union Gre", "SE"]<-TradeSE[c("Greens:union")]
trade.PlotA[trade.PlotA$Variable=="Trade Union Left", "SE"]<-TradeSE[c("Left:union")]

trade.PlotA$Coding<-"member (1), otherwise (0)"
trade.PlotA


# Coding: "otherwise (1), member (0)" 
TradeRSE<-sqrt(diag(solve(-TradeR$hessian)))

trade.PlotB<-data.frame(Variable=coef.namesT, Coefficient=0, SE=0)

trade.PlotB[trade.PlotB$Variable=="Intercept SPD", "Coefficient"]<-coef(TradeR)[c("SPD:(intercept)")]
trade.PlotB[trade.PlotB$Variable=="Intercept FDP", "Coefficient"]<-coef(TradeR)[c("FDP:(intercept)")]
trade.PlotB[trade.PlotB$Variable=="Intercept Gre", "Coefficient"]<-coef(TradeR)[c("Greens:(intercept)")]
trade.PlotB[trade.PlotB$Variable=="Intercept Left", "Coefficient"]<-coef(TradeR)[c("Left:(intercept)")]
trade.PlotB[trade.PlotB$Variable=="Trade Union SPD", "Coefficient"]<-coef(TradeR)[c("SPD:union2")]
trade.PlotB[trade.PlotB$Variable=="Trade Union FDP", "Coefficient"]<-coef(TradeR)[c("FDP:union2")]
trade.PlotB[trade.PlotB$Variable=="Trade Union Gre", "Coefficient"]<-coef(TradeR)[c("Greens:union2")]
trade.PlotB[trade.PlotB$Variable=="Trade Union Left", "Coefficient"]<-coef(TradeR)[c("Left:union2")]

trade.PlotB[trade.PlotB$Variable=="Intercept SPD", "SE"]<-TradeRSE[c("SPD:(intercept)")]
trade.PlotB[trade.PlotB$Variable=="Intercept FDP", "SE"]<-TradeRSE[c("FDP:(intercept)")]
trade.PlotB[trade.PlotB$Variable=="Intercept Gre", "SE"]<-TradeRSE[c("Greens:(intercept)")]
trade.PlotB[trade.PlotB$Variable=="Intercept Left", "SE"]<-TradeRSE[c("Left:(intercept)")]
trade.PlotB[trade.PlotB$Variable=="Trade Union SPD", "SE"]<-TradeRSE[c("SPD:union2")]
trade.PlotB[trade.PlotB$Variable=="Trade Union FDP", "SE"]<-TradeRSE[c("FDP:union2")]
trade.PlotB[trade.PlotB$Variable=="Trade Union Gre", "SE"]<-TradeRSE[c("Greens:union2")]
trade.PlotB[trade.PlotB$Variable=="Trade Union Left", "SE"]<-TradeRSE[c("Left:union2")]

trade.PlotB$Coding<-"otherwise (1), member (0)"
trade.PlotB

trade.Plot<-rbind(trade.PlotA,trade.PlotB)
trade.Plot

trade.Plot$Variable <- factor(trade.Plot$Variable, levels=coef.namesT)

# Plot
pdf(file="Figure1_Trade.pdf",width=6.1, height=3.7)
par(mar=c(0,0,2.5,0)) 

TradeP <- ggplot(trade.Plot, aes(color =rev(Coding), group=rev(Coding))) +  
  geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_pointrange(aes(x = Variable, y = Coefficient, ymin = Coefficient - SE*interval, ymax = Coefficient + SE*interval),
                  lwd = 1/2, position = position_dodge(width = 1/2),
                  shape = rep(c(21,22),10), fill = "WHITE") + 
                  scale_y_continuous(name="", limits=c(-5,3.2), breaks=c(-5,-4,-3,-2,-1,0,1,2,3)) + coord_flip() + theme_bw(base_size = 12) + 
                  theme(legend.position="none", legend.margin = margin(-1,-1,-1,-1), legend.spacing = unit(-1, "cm"), legend.title = element_blank(),) + 
                  labs(x="", y="") + scale_fill_grey(start = 0.6, end = 0.9) + scale_color_grey(start = 0.1, end = 0.3) +
                  guides(fill = guide_legend(override.aes = list(shape = c(21,22))),
                  colour = guide_legend(override.aes = list(size=0.9,linetype=0, shape = c(21,22)  ))) 
print(TradeP)

dev.off()